package org.com.ashen.web.timer;


import java.io.File;
import java.io.IOException;

import org.apache.http.entity.ContentType;
import org.com.ashen.service.util.FileUtil;
import org.com.ashen.web.constant.CommonUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fcibook.quick.http.QuickHttp;

@Component
public class AccessTokenTimer  {
	@Scheduled(cron = "0 0 */1 * * ? ")
	public void taskCycle() {
        System.out.println("aa");
        String client_id = "CB524068C8BF6FF86BEBC45CB3599A94";
        String client_secret = "C70FA1FCB310C3001A82AC96FB152F27";
        String refresh_token = "55b696e3-91ae-42d6-9851-6cc351c250b1";
        String url = "https://dopen.weimob.com/fuwu/b/oauth2/token?grant_type=refresh_token&client_id=" + client_id + "&client_secret=" + client_secret + "&refresh_token=" + refresh_token;
        String accessToke = new QuickHttp()
                .url(url)
                .setContentType(ContentType.APPLICATION_JSON)
                .addHeader("charset", "utf-8")
                .post()
                .text();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1 = JSON.parseObject(accessToke);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("weimengAccessToken", jsonObject1.getString("access_token"));


        String wxclient_id = "wxed8617988c573ed8";
        String wxclient_secret = "11755dc24d97d14cfa93629bae8dbf35";
        String wxurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wxclient_id + "&secret=" + wxclient_secret;
        String wxaccessToke = new QuickHttp()
                .url(wxurl)
                .setContentType(ContentType.TEXT_XML)
                .addHeader("charset", "utf-8")
                .get()
                .text();
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2 = JSON.parseObject(wxaccessToke);
        jsonObject.put("wxAccessToken", jsonObject2.getString("access_token"));
        String jsonString = JSON.toJSONString(jsonObject);
        try {
            FileUtil.touch(new File(CommonUtils.FILE_PATH));
            FileUtil.genModuleTplClear(CommonUtils.FILE_PATH, jsonString);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
	}
